(function($) {
    $.fn.extend({
        defaults : {
            wrap: $("ul.display"),
            parentWrap:$("div.display"),
            saveBtn:$("#submit"),
            addClass:'',
            width: 180,
            height: 150,
            f : null,
            src: null,
            imgInfo:[],
            allowedType: ['png', 'jpg', 'jpeg', 'gif', 'bmp','mp4','mp3','doc','docx'],
            imageType:['png', 'jpg', 'jpeg', 'gif', 'bmp'],
            maxSize : "5M",
            saveUrl:'',
            delUrl:'',
            savefunc:function (arr) {
                return false;
            }
        },
        fileJson:null,
        filetype:null,
        mergeOption:null,
        upLoad: function (options) {
            var self = this;
            this.mergeOption = $.extend(this.defaults, options);
            this.mergeOption.parentWrap = this.mergeOption.wrap.parent();
            if(this.mergeOption.wrap.children.length){
                this.mergeOption.parentWrap.show();
            }
            if(this.mergeOption.imgInfo.length){
                $.each(this.mergeOption.imgInfo,function (i,item) {
                    self.initSource(item);
                });
            }
            this.onChange();
            this.hoverfunc();
            this.delsource();
            this.mergeOption.saveBtn.on("click",function () {
                var res = false,Imgarr = self.mergeOption.imgInfo;
                if(self.mergeOption.savefunc && typeof self.mergeOption.savefunc == 'function'){
                    res = self.mergeOption.savefunc.call(self,Imgarr) || false;
                }
                return res;
            })
        },
        onChange:function () {
            var mergeOption = this.mergeOption,
                maxSize = mergeOption.maxSize,
                self = this;
            $(this).change(function () {
                if(this.files&&this.files[0]){
                    self.fileJson = this.files[0];
                    self.filetype = Bolt.getFileType(self.fileJson);
                    if((mergeOption.allowedType).indexOf(self.filetype) == -1){
                        Bolt.notify('error',"文件格式不合法只能上传图片视频音频doc文件");
                        return ;
                    }
                    if(self.fileJson.size>Bolt.sizeToByte(maxSize)){
                        Bolt.notify('error',"单个文件不能超过"+maxSize);
                        return ;
                    }
                    else{
                        var obj = new FileReader();//新建读取文件对象
                        obj.readAsDataURL( self.fileJson );//读取文件
                        obj.onload = function(){
                            var result = this.result;
                            mergeOption.imgInfo.push({id:"",title:self.fileJson.name,size:self.fileJson.size,type:self.filetype,src:result});
                            self.initSource(result,1);
                            $(this).val('')
                        }
                    }
                }
            })
        },
        initSource:function (result,flag) {
            //flag:true是添加，false是编辑
            var $Li = $("<li class='source'></li>"),$Source;
            if(['doc','docx'].indexOf(this.filetype) != -1||['doc','docx'].indexOf(result.type) != -1){
                $Source = $("i.iconfont");
            }
            else{
                if(this.mergeOption.imageType.indexOf(this.filetype) != -1||this.mergeOption.imageType.indexOf(result.type) != -1){
                    //    如果是图片
                    $Source = $("<img>")
                }
                else if(this.filetype=='mp3'||result.type=='mp3'){
                    //    如果是mp3
                    $Source = $('<audio  controls preload="none"></audio>');
                }
                else if(this.filetype=='mp4'||result.type=='mp4'){
                    //    如果是mp4
                    $Source = $('<video controls></video>');
                }
                if(flag){
                    $Source.attr('src',result);
                }
                else{
                    var pin_result='';
                    if(this.mergeOption.imageType.indexOf(result.type) != -1){
                        pin_result = 'data:image/'+(result.type=='jpg'?'jpeg':result.type)+';base64,'+result.value;
                    }
                    else if(result.type=='mp3'){
                        pin_result = 'data:audio/'+result.type+';base64,'+result.value;
                    }
                    else if(result.type=='mp4'){
                        pin_result = 'data:video/'+result.type+';base64,'+result.value;
                    }
                    $Source.attr('src',pin_result);
                }
            }
            $Source.width(this.mergeOption.width);
            $Source.height(this.mergeOption.height);

            var imgname = flag?(this.fileJson.name):(result.name);
            $Li.append($Source).append("<p class='clearfloat'><span>"+imgname+"</span><i class='del fr'>✖</i></p>")
            if(flag){
                $Li.data("id","");
                $Li.data("filetype",this.filetype);
            }
            else{
                $Li.data("id",result.id);
                $Li.data("filetype",result.type);
            }
            this.mergeOption.wrap.append($Li);
            this.mergeOption.parentWrap.show();
        },
        hoverfunc:function () {
            this.mergeOption.wrap.on("mouseenter mouseleave",">li.source",function (e) {
                if(e.type=='mouseenter'){
                    $(this).find(">p").slideDown(500)
                }else if(e.type=='mouseleave'){
                    $(this).find(">p").slideUp(500)
                }
            })
        },
        delsource:function () {
            var self = this;
            this.mergeOption.wrap.on("click",">li>p>i.del",function () {
                var sourceId = $(this).parents("li.source").data("id"),
                    $Li = $(this).parents("li.source"),
                    Li_index = $Li.index();
                if(sourceId){
                    Bolt.ajax('doajax', {
                            url:self.mergeOption.delUrl,
                            data:{id:sourceId},
                            loadingMask: true,
                            okCallback: function (res){
                                $Li.remove();
                                self.mergeOption.imgInfo.splice(Li_index,1);
                            }
                    })
                }
                else{
                    $Li.remove();
                    self.mergeOption.imgInfo.splice(Li_index,1);
                }
                if(!self.mergeOption.wrap.children("li.source").length){
                    self.mergeOption.parentWrap.hide();
                }
            })
        }
    });
    $.fn.extend({
        upload: $.fn.upLoad
    });
})(jQuery);